export function formatTime(time, format) {
    if (!time) {
        return '';
    }
    if (typeof time === 'string') {
        time = time.toString().replace(/-/g, '/'); // ios下new Date(2016-10-31)报错:invalid date...要改为: 2016/10/31样式
    }
    if (typeof time === 'number' && time.toString().length === 10) {
        time = parseInt(time + '000');
    }

    // 过去
    let stamp = new Date(time),
        cur = new Date(),
        year = stamp.getFullYear(),
        month = stamp.getMonth() + 1 > 9 ? stamp.getMonth() + 1 : '0' + (stamp.getMonth() + 1),
        day = stamp.getDate() > 9 ? stamp.getDate() : '0' + stamp.getDate(),
        hour = stamp.getHours() > 9 ? stamp.getHours() : '0' + stamp.getHours(),
        minute = stamp.getMinutes() > 9 ? stamp.getMinutes() : '0' + stamp.getMinutes(),
        sec = stamp.getSeconds() > 9 ? stamp.getSeconds() : '0' + stamp.getSeconds(),
        ms =
            stamp.getMilliseconds() < 100
                ? '0' + (stamp.getMilliseconds() < 10 ? '0' + stamp.getMilliseconds() : stamp.getMilliseconds())
                : stamp.getMilliseconds(),
        weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
        week = weeks[stamp.getDay()];
    if (!year) {
        return '0000年00月00日';
    }

    if (format) {
        format = format.replace('yyyy', year);
        format = format.replace('MM', month);
        format = format.replace('dd', day);
        format = format.replace('hh', hour);
        format = format.replace('mm', minute);
        format = format.replace('ss', sec);
        format = format.replace('ms', ms);

        if (year === cur.getFullYear() && stamp.getMonth() === cur.getMonth() && stamp.getDate() === cur.getDate()) {
            week = '今天';
        }
        format = format.replace('week', week);
    } else {
        format = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
    }

    return format;
}

export function specialTime(time) {
    if (!time) return '';
    if (typeof time === 'string') {
        time = time.toString().replace(/-/g, '/'); // ios下new Date(2016-10-31)报错:invalid date...要改为: 2016/10/31样式
    }
    if (typeof time === 'number' && time.toString().length === 10) {
        time = parseInt(time + '000');
    }
    let cur = new Date(),
        stamp = new Date(time),
        rtn = '';
    // console.log('filters.specialTime: ', cur.getFullYear(), stamp.getFullYear());
    if (
        cur.getFullYear() === stamp.getFullYear() &&
        cur.getMonth() === stamp.getMonth() &&
        cur.getDate() === stamp.getDate()
    ) {
        // today
        rtn = formatTime(time, 'hh:mm:ss');
    } else if (cur.getFullYear() === stamp.getFullYear()) {
        // 今年
        rtn = formatTime(time, 'MM-dd hh:mm');
    } else {
        rtn = formatTime(time, 'yyyy-MM-dd hh:mm');
    }
    return rtn;
}

export function getTomorrow(now) {
    const date = now ? new Date() : new Date(new Date().toLocaleDateString());
    date.setTime(date.getTime() + 3600 * 1000 * 24);
    return date;
}

export function get1DaysLater(now) {
    const date = now ? new Date() : new Date(new Date().toLocaleDateString());
    date.setTime(date.getTime() + 3600 * 1000 * 24 * 2);
    return date;
}

export function get2DaysLater(now) {
    const date = now ? new Date() : new Date(new Date().toLocaleDateString());
    date.setTime(date.getTime() + 3600 * 1000 * 24 * 3);
    return date;
}

export function get3DaysLater(now) {
    const date = now ? new Date() : new Date(new Date().toLocaleDateString());
    date.setTime(date.getTime() + 3600 * 1000 * 24 * 4);
    return date;
}

export function getNextWeek(now) {
    const date = now ? new Date() : new Date(new Date().toLocaleDateString());
    date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
    return date;
}

export function getIntervalString(ms, forceSign) {
    if (typeof ms !== 'number') ms = 0;
    let t = ms + ' ms';

    if (Math.abs(ms) >= 1000) t = Math.floor(ms / 1000) + ' sec';
    if (Math.abs(ms) >= 60000) t = Math.floor(ms / 60000) + ' min';
    if (Math.abs(ms) >= 3600000) t = Math.floor(ms / 3600000) + ' h';

    return forceSign && ms > 0 ? '+' + t : t;
}

export function getDateString(ms) {
    var date = new Date(ms);

    var d = date.getDate();
    var m = date.getMonth() + 1;
    // var y = date.getFullYear().toString().slice(-2);
    var y = date.getFullYear().toString();

    if (d < 10) d = '0' + d;
    if (m < 10) m = '0' + m;

    // return d + "." + m + "." + y;
    return y + '.' + m + '.' + d;
}

export function getTimeString(ms, UTC) {
    var date = new Date(ms);

    var h = UTC ? date.getUTCHours() : date.getHours();
    var m = UTC ? date.getUTCMinutes() : date.getMinutes();
    var s = UTC ? date.getUTCSeconds() : date.getSeconds();

    if (h < 10) h = '0' + h;
    if (m < 10) m = '0' + m;
    if (s < 10) s = '0' + s;

    return h + ':' + m + ':' + s;
}

export function getDateTimeString(ms) {
    return getDateString(ms) + ' ' + getTimeString(ms);
}
